<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="评分" prop="score">
        <el-input v-model="queryParams.score" placeholder="请输入评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:lawyer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:lawyer:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:lawyer:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:lawyer:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="lawyerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <!-- <el-table-column label="头像" align="center" prop="avator" /> -->
      <el-table-column label="头像">
        <template slot-scope="scope">
          <img class="avatar" :src="scope.row.avatar">
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="nickName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="执行证号" align="center" prop="licenseno" />
      <el-table-column label="执行年数" align="center" prop="workYear" />
      <el-table-column label="律师简介" align="center" prop="content" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="电话号码" align="center" prop="phonenumber" />
      <el-table-column label="电话咨询价格" align="center" prop="dhPrice" />
      <el-table-column label="图文咨询价格" align="center" prop="imgPrice" />
      <el-table-column label="在线咨询价格" align="center" prop="onlinePrice" />
      <el-table-column label="电话咨询订单数量" align="center" prop="dhNum" />
      <el-table-column label="图文咨询订单数量" align="center" prop="imgNum" />
      <el-table-column label="在线咨询订单数量" align="center" prop="onlineNum" />
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="审核状态" align="center" prop="shState" />
      <el-table-column label="浏览量" align="center" prop="view" />
      <el-table-column label="收藏数量" align="center" prop="collectNum" />
      <el-table-column label="评论数量" align="center" prop="evaluate" />
      <el-table-column label="拒绝原因" align="center" prop="jujueContent" />
      <!-- <el-table-column label="身份证照片" align="center" prop="sfzImage" /> -->
      <el-table-column label="身份证照片">
        <template slot-scope="scope">
          <img class="sfzImage" :src="scope.row.sfzImage">
        </template>
      </el-table-column>
      <!-- <el-table-column label="律师执照照片" align="center" prop="licenseImage" /> -->
      <el-table-column label="律师执照照片">
        <template slot-scope="scope">
          <img class="licenseImage" :src="scope.row.licenseImage">
        </template>
      </el-table-column>
      <el-table-column label="电话咨询状态" align="center" prop="dhStatue" />
      <el-table-column label="图文咨询状态" align="center" prop="imageStatue" />
      <el-table-column label="在线咨询状态" align="center" prop="onlineStatue" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:lawyer:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:lawyer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改律师信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :rules="[{
          validator: validateEmail,
          trigger: 'blur'
        }]">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phonenumber" :rules="[{
          validator: validatePhone,
          trigger: 'blur'
        }]">
          <el-input v-model="form.phonenumber" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="执行证号" prop="licenseno">
          <el-input v-model="form.licenseno" placeholder="请输入执行证号" />
        </el-form-item>
        <el-form-item label="执行年数" prop="workYear">
          <el-input v-model="form.workYear" placeholder="请输入执行年数" />
        </el-form-item>
        <el-form-item label="律师简介">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="电话咨询价格" prop="dhPrice" :rules="[{
          validator: validateNumber,
          trigger: 'blur'
        }]">
          <el-input v-model="form.dhPrice" placeholder="请输入电话咨询价格" />
        </el-form-item>
        <el-form-item label="图文咨询价格" prop="imgPrice" :rules="[{
          validator: validateNumber,
          trigger: 'blur'
        }]">
          <el-input v-model="form.imgPrice" placeholder="请输入图文咨询价格" />
        </el-form-item>
        <el-form-item label="在线咨询价格" prop="onlinePrice" :rules="[{
          validator: validateNumber,
          trigger: 'blur'
        }]">
          <el-input v-model="form.onlinePrice" placeholder="请输入在线咨询价格" />
        </el-form-item>
        <el-form-item label="电话咨询订单数量" prop="dhNum">
          <el-input v-model="form.dhNum" placeholder="请输入电话咨询订单数量" />
        </el-form-item>
        <el-form-item label="图文咨询订单数量" prop="imgNum">
          <el-input v-model="form.imgNum" placeholder="请输入图文咨询订单数量" />
        </el-form-item>
        <el-form-item label="在线咨询订单数量" prop="onlineNum">
          <el-input v-model="form.onlineNum" placeholder="请输入在线咨询订单数量" />
        </el-form-item>
        <el-form-item label="评分" prop="score">
          <el-select v-model="form.score" placeholder="请选择评分">
            <el-option label="1" :value="1"></el-option>
            <el-option label="2" :value="2"></el-option>
            <el-option label="3" :value="3"></el-option>
            <el-option label="4" :value="4"></el-option>
            <el-option label="5" :value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="shState">
          <el-select v-model="form.shState" placeholder="请选择审核状态">
            <el-option label="待审核" :value="1"></el-option>
            <el-option label="通过" :value="2"></el-option>
            <el-option label="不通过" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="浏览量" prop="view">
          <el-input v-model="form.view" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="收藏数量" prop="collectNum">
          <el-input v-model="form.collectNum" placeholder="请输入收藏数量" />
        </el-form-item>
        <el-form-item label="评论数量" prop="evaluate">
          <el-input v-model="form.evaluate" placeholder="请输入评论数量" />
        </el-form-item>
        <el-form-item label="拒绝原因">
          <editor v-model="form.jujueContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="电话咨询状态" prop="dhStatue">
          <el-select v-model="form.dhStatue" placeholder="请选择咨询状态">
            <el-option label="是" value="yes"></el-option>
            <el-option label="否" value="no"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="图文咨询状态" prop="imageStatue">
          <el-select v-model="form.imageStatue" placeholder="请选择咨询状态">
            <el-option label="是" value="yes"></el-option>
            <el-option label="否" value="no"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="在线咨询状态" prop="onlineStatue">
          <el-select v-model="form.onlineStatue" placeholder="请选择咨询状态">
            <el-option label="是" value="yes"></el-option>
            <el-option label="否" value="no"></el-option>
          </el-select>
        </el-form-item>
        <!-- 上传头像 -->
        <el-form-item label="头像上传">
          <el-upload class="avatar-uploader" action="http://localhost:8080/common/upload" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="scope.row.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 上传身份证照片 -->
        <el-form-item label="身份证照片上传">
          <el-upload class="sfzImg-uploader" action="http://localhost:8080/common/upload" :show-file-list="false"
            :on-success="handleSfzImageSuccess" :before-upload="beforeSfzImageUpload">
            <img v-if="sfzImageUrl" :src="scope.row.sfzImage" class="sfzImg">
            <i v-else class="el-icon-plus sfzImg-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 上传律师执照照片 -->
        <el-form-item label="律师执照照片上传">
          <el-upload class="licenseImg-uploader" action="http://localhost:8080/common/upload" :show-file-list="false"
            :on-success="handleLicenseImageSuccess" :before-upload="beforeLicenseImageUpload">
            <img v-if="licenseImageUrl" :src="licenseImage" class="licenseImg">
            <i v-else class="el-icon-plus licenseImg-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLawyer, getLawyer, delLawyer, addLawyer, updateLawyer } from "@/api/system/lawyer";
import userAvatar from '@/views/system/user/profile/userAvatar'
export default {
  name: "Lawyer",
  name: "Proile",
  components: { userAvatar },
  data() {
    return {
      //头像地址
      imageUrl: '',
      //身份证照片url
      sfzImageUrl: '',
      //律师执照照片地址
      licenseImageUrl: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 律师信息表格数据
      lawyerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        goodareasId: null,
        score: null,
      },
      // 表单参数
      form: {
        userName: '',
        nickName: '',
        sex: '',
        phonenumber: '',
        dhPrice: '',
        licenseno: '',
        workYear: '',
        content: '',
        imgPrice: '',
        onlinePrice: '',
        email: '',
        shState: '',
        dhNum: '',
        shState: '',
        dhStatue: '',
        imageStatue: '',
        jujueContent: '',
        view: '',
        collectNum: '',
        evaluate: '',
        dhNum: '',
        imgNum: '',
        onlineNum: '',
        avatar: '',
        sfzImage: '',
        licenseImage: ''
      },

      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "执行证号不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", validator: this.validateEmail, trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: "电话号码不能为空", validator: this.validatePhone, trigger: 'blur' }
        ],
        licenseno: [
          { required: true, message: "执行证号不能为空", trigger: "blur" }
        ],
        workYear: [
          { required: true, message: "执行年数不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "律师简介不能为空", trigger: "blur" }
        ],
        dhPrice: [
          { required: true, message: "电话咨询价格不能为空", validator: this.validateNumber, trigger: 'blur' }
        ],
        imgPrice: [
          { required: true, message: "图文咨询价格不能为空", validator: this.validateNumber, trigger: "blur" }
        ],
        goodareasId: [
          { required: true, message: "擅长领域擅长领域id不能为空", trigger: "change" }
        ],
        onlinePrice: [
          { required: true, message: "在线咨询价格不能为空", validator: this.validateNumber, trigger: "blur" }
        ],
        dhNum: [
          { required: true, message: "电话咨询订单数量不能为空", trigger: "blur" }
        ],
        imgNum: [
          { required: true, message: "图文咨询订单数量不能为空", trigger: "blur" }
        ],
        onlineNum: [
          { required: true, message: "在线咨询订单数量不能为空", trigger: "blur" }
        ],
        score: [
          { required: false, message: "评分不能为空", trigger: "blur" }
        ],
        shState: [
          { required: true, message: "审核状态0审核中1审核拒绝2审核通过不能为空", trigger: "change" }
        ],
        view: [
          { required: false, message: "浏览量不能为空", trigger: "blur" }
        ],
        collectNum: [
          { required: false, message: "收藏数量不能为空", trigger: "blur" }
        ],
        evaluate: [
          { required: false, message: "评论数量不能为空", trigger: "blur" }
        ],
        jujueContent: [
          { required: false, message: "拒绝原因不能为空", trigger: "blur" }
        ],
        dhStatue: [
          { required: false, message: "电话咨询状态不能为空", trigger: "blur" }
        ],
        imageStatue: [
          { required: false, message: "图文咨询状态不能为空", trigger: "blur" }
        ],
        onlineStatue: [
          { required: false, message: "在线咨询状态不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //头像上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log("res======", res.url)
      this.form.avatar = res.url;

      //
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //身份证上传
    handleSfzImageSuccess(res, file) {
      this.sfzImageUrl = URL.createObjectURL(file.raw);
      this.form.sfzImage = res.url;
    },
    beforeSfzImageUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //律师执照上传
    handleLicenseImageSuccess(res, file) {
      this.licenseImageUrl = URL.createObjectURL(file.raw);
      this.form.licenseImage = res.url;
    },
    beforeLicenseImageUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //验证有效的数字，防止输入字母或符号
    validateNumber(rule, value, callback) {
      const regex = /^\d+(\.\d+)?$/;
      if (regex.test(value)) {
        callback();
      } else {
        callback(new Error('请输入有效的数字'));
      }
    },
    //校验邮箱的规则
    validateEmail(rule, value, callback) {
      const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (!reg.test(value)) {
        callback(new Error('请输入有效的邮箱'));
      }
      callback();
    },
    // 验证手机号的规则
    validatePhone(rule, value, callback) {
      const reg = /^1[3456789]\d{9}$/;
      if (!reg.test(value)) {
        callback(new Error('请输入有效的手机号码'));
      }
      callback();
    },
    /** 查询律师信息列表 */
    getList() {
      this.loading = true;
      listLawyer(this.queryParams).then(response => {
        console.log("response----", response)
        this.lawyerList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        nickName: null,
        sex: null,
        email: null,
        phonenumber: null,
        licenseno: null,
        workYear: null,
        content: null,
        dhPrice: null,
        imgPrice: null,
        goodareasId: null,
        onlinePrice: null,
        dhNum: null,
        imgNum: null,
        onlineNum: null,
        score: null,
        shState: null,
        view: null,
        collectNum: null,
        evaluate: null,
        jujueContent: null,
        dhStatue: null,
        imageStatue: null,
        onlineStatue: null,
        evaluateId: null,
        createTime: null,
        updateTime: null,
        avatar: null,
        sfzImage: null,
        licenseImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加律师信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLawyer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改律师信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLawyer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form)
            addLawyer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除律师信息编号为"' + ids + '"的数据项？').then(function () {
        return delLawyer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/lawyer/export', {
        ...this.queryParams
      }, `lawyer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.sfzImg-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.sfzImg-uploader .el-upload:hover {
  border-color: #409EFF;
}

.sfzImg-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.sfzImg {
  width: 178px;
  height: 178px;
  display: block;
}

.licenseImg-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.licenseImg-uploader .el-upload:hover {
  border-color: #409EFF;
}

.licenseImg-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.licenseImg {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 10px;
}

.sfzImage {
  width: 40px;
  height: 40px;
  border-radius: 10px;
}

.licenseImage {
  width: 40px;
  height: 40px;
  border-radius: 10px;
}
</style>